package singleton;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-24 13:20
 * @notify
 * @version 1.0
 */

import junit.framework.TestCase;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class SingletonTest extends TestCase {
    public void testCreate() {
        EagerSingleton e1 = EagerSingleton.getInstance();
        EagerSingleton e2 = EagerSingleton.getInstance();
        assertTrue(e1 == e2);

        LazySingleton l1 = LazySingleton.getInstance();
        LazySingleton l2 = LazySingleton.getInstance();
        assertTrue(l1==l2);

    }

    public void testKeyGenerator() {
        KeyGenerator instance = KeyGenerator.getInstance();
        assertEquals(0, instance.getNextKey());
        assertEquals(1, instance.getNextKey());
        assertEquals(2, instance.getNextKey());
    }

    public void testReadDB() throws Exception {
        Properties properties = new Properties();
        InputStream in = new FileInputStream("C:\\JavaPattern\\src\\singleton\\KEYDB.properties");
        properties.load(in);
        assertTrue(properties.getProperty("key") != null);
        properties.setProperty("key", "aaa");
    }

    public void testGetSingNextKey() throws Exception {
        KeyGenerator instance = KeyGenerator.getInstance();
        int singNextKey = instance.getSingNextKey();
        assertEquals(1, singNextKey);
        int singNextKey2 = instance.getSingNextKey();
        assertEquals(2, singNextKey2);
        int singNextKey3 = instance.getSingNextKey();
        assertEquals(3, singNextKey3);
    }

    public void testGetPoolKey() {
        KeyGenerator instance = KeyGenerator.getInstance();
        for (int i = 1; i < 100; i++) {
            int poolKey = instance.getPoolKey();
        }
    }
}
